<?php

namespace App\Transformer;


use App\Models\CustomerRecords;
use League\Fractal\TransformerAbstract;

class CustomerRecordsTransformer extends TransformerAbstract
{
    protected $defaultIncludes = ['user', 'files'];

    public function transform(CustomerRecords $data)
    {
        return [
            'id' => $data->id,
            'user_id' => $data->user_id,
            'customer_id' => $data->customer_id,
            'type' => $data->type,
            'type_text' => $data->getTypeText(),
            'result' => $data->result,
            'result_text' => $data->getResultText(),
            'remark' => $data->remark,
            'created_at' => $data->created_at->toDateTimeString(),
        ];
    }

    public function includeUser(CustomerRecords $data)
    {
        if ($data->relationLoaded('user') && $data->user) {
            return $this->item($data->user, new UserTransformer(), false);
        }
    }

    public function includeFiles(CustomerRecords $data)
    {
        if ($data->relationLoaded('files') && $data->files) {
            return $this->collection($data->files, new CustomerRecordsFileTransformer(), false);
        }
    }

}
